//
//  TKTableViewController.m
//  TaoKeClient
//
//  Created by YueWen on 2017/10/21.
//  Copyright © 2017年 YueWen. All rights reserved.
//

#import "NWTableViewController.h"
#import <Masonry/Masonry.h>
#import "QDRefreshFooter.h"
#import "QDRefreshHeader.h"
#import <RITLKit/RITLKit.h>

typedef NSString TableViewLayoutConstraintKey;

//static const TableViewLayoutConstraintKey* TableViewTop = @"TableViewTop";
//static const TableViewLayoutConstraintKey* TableViewBottom = @"TableViewBottom";

@interface NWTableViewController ()

@property (nonatomic, assign)BOOL headerRefresh_Enable;
@property (nonatomic, assign)BOOL footerRefresh_Enable;


#pragma mark - NothingView
/// 放置搜不到图标的视图
@property (nonatomic, strong) UIImageView * iconImageView;
/// 放置搜不到的语言描述
@property (nonatomic, strong) UILabel * iconLabel;


@end

@implementation NWTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self registerTableViewCellsAndReusableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    
    [self.view addSubview:self.tableView];
    [self makeTableViewConstraints];
    [self tk_addRefreshComponent];//追加刷新组件
}

- (void)registerTableViewCellsAndReusableView {
    
}


- (void)makeTableViewConstraints
{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(self.tableViewConstraintInsets.left);
        make.right.inset(self.tableViewConstraintInsets.right);
        make.bottom.inset(self.tableViewConstraintInsets.bottom);
        make.top.offset(self.tableViewConstraintInsets.top);
    }];
}

- (void)loadPropertysAtInitialization
{
    [super loadPropertysAtInitialization];
    
    self.tableViewConstraintInsets = UIEdgeInsetsZero;
    self.currentPage = 0;
    self.totalPage = 0;
    self.headerRefreshEnable = true;
    self.footerRefreshEnable = true;
    self.titleForFooterRefreshWithNoMoreData = @"没有更多数据了";
    
    self.tableView = ({
        
        NWTableView *tableView = [[NWTableView alloc]initWithFrame:CGRectZero style:self.tableViewStyle];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        tableView.showsVerticalScrollIndicator = false;
        tableView.showsHorizontalScrollIndicator = false;
        
        tableView;
    });
    
    self.tableView.backgroundView = ({
        
        UIView *view = [UIView new];
        view.backgroundColor = self.tableViewBackgroundColor;
        
        view;
    });
}


- (UIColor *)tableViewBackgroundColor
{
    return UIColor.whiteColor;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)didBottom
{
    return self.totalPage < self.currentPage;
}

- (BOOL)isAll
{
    return self.totalPage < self.currentPage;
}


- (void)checkRefreshFooterIsEndRefreshingWithNoMoreData
{
    if (self.totalPage < self.currentPage) {
        [self.refreshFooter endRefreshingWithNoMoreData];
    }
}


- (UITableViewStyle)tableViewStyle
{
    return UITableViewStyleGrouped;
}


#pragma mark - UITableViewDatasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UITableViewCell new];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:true];
}



#pragma mark - 上拉下拉刷新

- (BOOL)headerRefreshEnable
{
    return self.headerRefresh_Enable;
}

- (BOOL)footerRefreshEnable
{
    return self.footerRefresh_Enable;
}

- (void)setHeaderRefreshEnable:(BOOL)headerRefreshEnable
{
    self.headerRefresh_Enable = headerRefreshEnable;
}

- (void)setFooterRefreshEnable:(BOOL)footerRefreshEnable
{
    self.footerRefresh_Enable = footerRefreshEnable;
}



/// 添加刷新组件
- (void)tk_addRefreshComponent
{
    [self installAllRefresh];
}

-(void)installHeaderRefresh
{
    if (!self.tableView.mj_header && self.headerRefreshEnable) {
        
        self.tableView.mj_header = self.refreshHeader;
    }
}

-(void)installFooterRefresh
{
    if (!self.tableView.mj_footer && self.footerRefreshEnable) {
        
        self.tableView.mj_footer = self.refreshFooter;
    }
}

-(void)installAllRefresh
{
    [self installHeaderRefresh];
    [self installFooterRefresh];
}

-(void)unstallHeaderRefresh
{
    if (self.tableView.mj_header) {
        
        self.tableView.mj_header = nil;
        [self.tableView.mj_header removeFromSuperview];
    }
}

-(void)unstallFooterRefresh
{
    if (self.tableView.mj_footer) {
        
        self.tableView.mj_footer = nil;
        [self.tableView.mj_footer removeFromSuperview];
    }
}

-(void)unstallAllRefresh
{
    [self unstallHeaderRefresh];
    [self unstallFooterRefresh];
}


- (Class)classForRefreshHeader
{
    return [MJRefreshNormalHeader class];
}

- (Class)classForRefreshFooter
{
    return [MJRefreshBackStateFooter class];
}

- (void)headerRefreshBeginHandler
{
    [self.refreshFooter endRefreshingWithNoMoreData];
}

- (void)footerRefreshBeginHandler
{
    
}

- (void)endHeaderRefreshing
{
    if (self.tableView.mj_header && self.tableView.mj_header.isRefreshing) {
        [self.refreshHeader endRefreshing];
    }
}

- (void)endFooterRefreshing
{
    if (self.tableView.mj_footer && self.tableView.mj_footer.isRefreshing) {
        [self.refreshFooter endRefreshing];
    }
}

-(MJRefreshHeader *)refreshHeader
{
    if (!_refreshHeader) {
        
        __weak typeof(self) weakSelf = self;
        
        //初始化
        _refreshHeader = [self.classForRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf headerRefreshBeginHandler];
        }];
        
        if ([_refreshHeader isKindOfClass:MJRefreshNormalHeader.class]){
            MJRefreshNormalHeader *normalHeader = (MJRefreshNormalHeader *)_refreshHeader;
            normalHeader.stateLabel.font = RITLRegularFont(14);
            normalHeader.stateLabel.textColor = QDSColorDefaultSeparator;
            normalHeader.lastUpdatedTimeLabel.font = RITLRegularFont(14);
            normalHeader.lastUpdatedTimeLabel.textColor = QDSColorDefaultSeparator;
            normalHeader.automaticallyChangeAlpha = true;
            normalHeader.lastUpdatedTimeLabel.hidden = true;

        }
        
    }
    return _refreshHeader;
}

-(MJRefreshFooter *)refreshFooter
{
    if (!_refreshFooter) {
        
        __weak typeof(self) weakSelf = self;
        
        _refreshFooter = [self.classForRefreshFooter footerWithRefreshingBlock:^{
            [weakSelf footerRefreshBeginHandler];
        }];
        
        _refreshFooter.stateLabel.font = [UIFont systemFontOfSize:12];
        _refreshFooter.stateLabel.textColor = RITLSimpleColorFromIntRBG(179);
        [_refreshFooter setTitle:self.titleForFooterRefreshWithNoMoreData forState:MJRefreshStateNoMoreData];
    }
    
    return _refreshFooter;
}

-(NSString *)titleForFooterRefreshWithNoMoreData
{
    if (!_titleForFooterRefreshWithNoMoreData) {
        return @"-----我是有底线的-----";
    }
    
    return _titleForFooterRefreshWithNoMoreData;
}

//- (BOOL)autoResume
//{
//    return true;
//}


#pragma mark - NothingView

- (UIImage *)nothingImage { return nil; }
- (NSString *)nothingTitle { return nil; }


- (void)showNothingView:(BOOL)isShow {
    
    if (isShow) { //启用动画展示
        
        [UIView animateWithDuration:0.4 animations:^{
            self.iconImageView.hidden = !isShow;
            self.iconLabel.hidden = !isShow;
        }];
        
    }else {
        
        if (_iconLabel != nil && _iconImageView != nil) {//避免lazy的浪费加载
            self.iconImageView.hidden = !isShow;
            self.iconLabel.hidden = !isShow;
        }
    }
    
    self.tableView.hidden = isShow;
}


- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        
        UIImageView *imageView = UIImageView.new;
        imageView.clipsToBounds = true;
        imageView.hidden = true;
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.image = self.nothingImage;
        
        _iconImageView = imageView;
        
        //追加视图
        [self.view addSubview:imageView];
        
        //追加约束
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(160);
            make.centerX.offset(0);
//            make.bottom.equalTo(self.iconLabel.mas_top).inset(4);
            make.centerY.offset(self.nothingImageOffset);
        }];
        
    }
    return _iconImageView;
}


- (UILabel *)iconLabel {
    if (!_iconLabel) {
        
        UILabel *label = UILabel.new;
        label.textColor = QDSColorDefaultGray2;
        label.font = RITLRegularFont(15);
        label.text = self.nothingTitle;
        label.hidden = true;
        
        _iconLabel = label;
        
        //追加视图
        [self.view addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.offset(3.5);
            make.top.equalTo(self.iconImageView.mas_bottom);
            make.centerX.offset(0);
            make.height.mas_equalTo(22);
        }];
        
    }
    return _iconLabel;
}

- (CGFloat)nothingImageOffset {
    return -42.5;
}


@end
